package bigNumbers;

import java.util.ArrayList;

public class Number_old
{
	private int digitPerChunk = 7;
	private String number = "";
	private ArrayList<String> chunks = new ArrayList<String>();
	private int pointPosition = 0;
	private int chunkNumber = 0;
	private int numberLenght = 0;
	private int decimalsLenght = 0;
	private int intereLenght = 0;
	private int sign =1;

	public Number_old(String number)
	{
		this.number = number;
		reChunk();
	}

	public Number_old()
	{
	};

	private void reChunk()
	{
		if(number.startsWith("-")){
			sign=-1;
			number=number.substring(1);
		}
		chunkNumber = number.length() / digitPerChunk;
		chunks = new ArrayList<String>();
		pointPosition = numberLenght;
		int i;

		// if(number.contains(".")){
		// pointPosition = number.indexOf(".");
		// number=number.replace(".", "");
		// }

		for (i = 0; i <= chunkNumber - 1; i++)
		{
			String stringChunk = number.substring((i) * digitPerChunk, (i + 1) * digitPerChunk);
			chunks.add(stringChunk);
			if (stringChunk.contains("."))
			{
				pointPosition = i * digitPerChunk + stringChunk.indexOf(".");
			}
		}
		if (i * digitPerChunk < number.length())
		{
			String stringChunk = number.substring((i) * digitPerChunk);
			chunks.add(stringChunk);
			if (stringChunk.contains("."))
			{
				pointPosition = i * digitPerChunk + stringChunk.indexOf(".");
			}
		}

		numberLenght = number.length();// + (pointPosition > 0 ? 1 : 0);
		decimalsLenght = numberLenght - pointPosition;
		intereLenght = numberLenght - decimalsLenght;

	}

	public int getDigitPerChunk()
	{
		return digitPerChunk;
	}

	public void setDigitPerChunk(int digitPerChunk)
	{
		this.digitPerChunk = digitPerChunk;
		reChunk();
	}

	public String getNumber()
	{
		return number;
	}

	public void setNumber(String number)
	{
		this.number = number;
		reChunk();
	}

	/**
	 * Give the position of the decimal Point
	 * 
	 * @return
	 */
	public int getPointPosition()
	{
		return pointPosition;
	}

	public int getNumberLenght()
	{
		return numberLenght;
	}

	public int getDecimalsLenght()
	{
		return decimalsLenght;
	}

	public Number_old align(Number_old n)
	{
		String zeroNumber = "";
		if (n.getIntereLenght() > this.getIntereLenght())
		{
			zeroNumber = getZeros(n.getIntereLenght() - this.getIntereLenght());
			String newNumber = zeroNumber + this.number;
			this.setNumber(newNumber);
		}
		else if (n.getIntereLenght() < this.getIntereLenght())
		{
			zeroNumber = getZeros(-n.getIntereLenght() + this.getIntereLenght());
			String newNumber = zeroNumber + n.getNumber();
			n.setNumber(newNumber);
		}

		String zeroDecimal = "";
		if (n.getDecimalsLenght() > this.getDecimalsLenght())
		{
			zeroDecimal = getZeros(n.getDecimalsLenght() - this.getDecimalsLenght());
			String newNumber = this.number + zeroDecimal;
			this.setNumber(newNumber);
		}
		else if (n.getDecimalsLenght() < this.getDecimalsLenght())
		{
			zeroDecimal = getZeros(-n.getDecimalsLenght() + this.getDecimalsLenght());
			String newNumber = n.getNumber() + zeroDecimal;
			n.setNumber(newNumber);
		}

		return n;
	}

	private String getZeros(int n)
	{
		if (n == 0)
			return "";
		StringBuffer res = new StringBuffer();
		for (int i = 0; i < n; i++)
		{
			res.append("0");
		}
		return res.toString();
	}

	@Override
	public String toString()
	{
		StringBuffer res = new StringBuffer((sign<0?"-":"+"));
		for (String d : chunks)
		{
			res.append(d);
			res.append(" ");
		}
		// String str = res.toString().substring(0,
		// pointPosition)+"."+res.toString().substring( pointPosition);
		return res.toString();
	}

	public int getIntereLenght()
	{
		return intereLenght;
	}

	public void setIntereLenght(int intereLenght)
	{
		this.intereLenght = intereLenght;
	}

	public ArrayList<String> getChunks()
	{
		return chunks;
	}

	public Number_old add(Number_old numb)
	{
		numb = this.align(numb);
		System.out.println(this);
		System.out.println(numb);
		int r = 0;
		String newNumb = "";
		System.out.println(chunks.size());
		for (int i = chunks.size(); i > 0; i--)
		{
			String aStr = this.getChunks().get(i - 1);
			String bStr = numb.getChunks().get(i - 1);
			int chunkLen = aStr.length();
			String cStr="";
			if (aStr.contains("."))
			{
				double a = Double.parseDouble(aStr);
				double b = Double.parseDouble(bStr);
				double decimals=Double.parseDouble(("1e-"+(chunkLen-aStr.indexOf(".")-1)))*r;
				cStr = (a*this.getSign()+b*numb.getSign()+decimals)+"";
				r=0;
				
			}
			else
			{
				int a = Integer.parseInt(aStr);
				int b = Integer.parseInt(bStr);
				cStr = (a*this.getSign()+b*numb.getSign()+r)+"";
				r=0;				
			}
			if (cStr.length()<chunkLen){
				cStr=getZeros(chunkLen-cStr.length())+cStr;
			}else if(cStr.length()>chunkLen){
				cStr=cStr.substring(1);
				r=1;
			}
			if(cStr.endsWith(".0")){
				cStr=cStr.substring(0,cStr.length()-1);
			}
			newNumb=cStr+newNumb;
		}
		//newNumb = newNumb.substring(0, pointPosition) + "." + newNumb.substring(pointPosition);
		if(r==1)
			newNumb="1"+newNumb;
		return new Number_old(newNumb);
	}

	public int getSign()
	{
		return sign;
	}

	public void setSign(int sign)
	{
		this.sign = sign;
	}

	

}
